package spider;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;

import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 多线程刷简书浏览量
 */
public class brushBrowseFlow {

    private static final ThreadPoolExecutor fixedThreadPool= (ThreadPoolExecutor) Executors.newFixedThreadPool(50);


    public static void main(String[] args) {
        final String url="https://www.jianshu.com/shakespeare/notes/ad6ca0e8db2f/mark_viewed";
        final String referer="https://www.jianshu.com/p/ad6ca0e8db2f";

        //打开默认浏览器打开页面
//        try {
//            Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }


        int i=0;
        while(i<2){
            i++;
            System.out.println("i="+i);
            fixedThreadPool.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        HttpClient client = new HttpClient();
                        PostMethod method = new PostMethod(url);
                        method.setRequestHeader("Referer", referer);
                        RequestEntity entity = new StringRequestEntity("{\"fuck\":1}", "application/json;", "utf-8");
                        method.setRequestEntity(entity);
//                        System.out.println(method);
                        client.executeMethod(method);
                        //打印服务器返回的状态
                        System.out.println(method.getStatusLine());
                        //释放连接
                        method.releaseConnection();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
        fixedThreadPool.shutdown();
    }
}
